using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
    /// <summary>
    /// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
    /// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
    /// </summary>
    internal class ActionLogsEF
    {
        public ActionLogsEF(DbContext dbContext)
        {
            actionLogsServerEF = new ActionLogsServerEF(dbContext);
            sysAdminsServerEF = new SysAccountsServerEF(dbContext);
        }
        public ActionLogsServerEF actionLogsServerEF { get; set; }
        public SysAccountsServerEF sysAdminsServerEF { get; set; }
    }
    [InjectClass]
    public class ActionLogsServer : IActionLogsServer
    {
        private readonly ActionLogsEF _actionLogsEF;//包含所有所需仓库表实现的对象
                                                    // 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
        public ActionLogsServer(DbContext dbContext)
        {
            _actionLogsEF = new ActionLogsEF(dbContext);
        }
        public int Add(ActionLogs log)
        {
            return _actionLogsEF.actionLogsServerEF.Add(log);
        }
        public async Task<ResponseApiDto> PaginationAsync(int page, int limit, string key)
        {
            int total = 0;
            if (key != null)
            {
                key = await _actionLogsEF.sysAdminsServerEF.Query(d => d.AName.Contains(key)).Select(d => d.AId).SingleOrDefaultAsync();
            }
            key = key ?? "";
            List<ResponseActionLogsDto> data = new List<ResponseActionLogsDto>();
            List<ActionLogs> actionLogss = await _actionLogsEF.actionLogsServerEF.Pagination1(d => d.AlAdminId.Contains(key), false, d => d.AlCreateTime, out total, page, limit).ToListAsync();
            foreach (var actionLogs in actionLogss)
            {
                data.Add(new ResponseActionLogsDto
                {
                    adminId = actionLogs.AlAdminId,
                    adminName = await _actionLogsEF.sysAdminsServerEF.Query(d => d.AId == actionLogs.AlAdminId).Select(d => d.AName).SingleOrDefaultAsync(),
                    affected = actionLogs.AlAffectedRows,
                    createTime = actionLogs.AlCreateTime,
                    createTimeStr=Tools.TimeToString(actionLogs.AlCreateTime),
                    id = actionLogs.AlId,
                    ip = actionLogs.AlIp,
                    message = actionLogs.AlMessage,
                    method = actionLogs.AlRequestMethod,
                    @params = actionLogs.AlRequestParams,
                    url = actionLogs.AlRequestUrl,
                });
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
    }
}
